//
//  HWTopicsDetiallViewController.m
//  HappyWeekend
//
//  Created by 赵洁 on 16/1/13.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWTopicsDetiallViewController.h"
#import "HWTopicsDetiallFootView.h"
#import "OTCover.h"
#import "HWLoginAndRegisterViewController.h"
#import "HWCommentViewController.h"
@interface HWTopicsDetiallViewController ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate,UMSocialUIDelegate>
@property(nonatomic,strong)HWTopicsDetiallFootView *footView;
@property(nonatomic,strong)OTCover *test;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,assign)CGFloat hegith;
@property(nonatomic,assign)BOOL isfirstTap;
@end

@implementation HWTopicsDetiallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationItem.title = @"专题详情";
    _isfirstTap = NO;
       _webView  = [[UIWebView alloc]initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight)];
    _webView.delegate = self;
    
    _webView.scrollView.bounces = NO;
    [self addTapgest];
    [self addTableView];
    [self setUpdata];
    [self addBottomView];

}

- (void)setUpdata
{
    NSString *str = [NSString stringWithFormat:@"http://api.guozhoumoapp.com/v1/posts/%@",_model.ID];
    [HWRequestManager requestWithUrl:str parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        [_model setValuesForKeysWithDictionary:dataDic];
        
        NSString *str = [NSString stringWithFormat:@"<head><style>img{width:340px !important;}</style></head>%@",_model.content_html];
        [_webView loadHTMLString:str baseURL:nil];
        
        [_test.tableView reloadData];
        
        _footView.likeLable.text = [NSString stringWithFormat:@"%@",_model.likes_count];
        _footView.shareLable.text = [NSString stringWithFormat:@"%@",_model.shares_count];
        _footView.commentLable.text = [NSString stringWithFormat:@"%@",_model.comments_count];
    } failedWithError:^(NSError *connectionError) {
        
    }];
}
- (void)addBottomView
{
    _footView = [[HWTopicsDetiallFootView alloc]initWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50)];
    _footView.backgroundColor = [UIColor colorWithRed:202 green:202 blue:222 alpha:1];
     [self.view addSubview:_footView];
    [_footView.likeButton addTarget:self action:@selector(likeButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_footView.shareButton addTarget:self action:@selector(shareButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_footView.commentButton addTarget:self action:@selector(commentButton:) forControlEvents:(UIControlEventTouchUpInside)];

}
//bottomview三个button点击方法
- (void)likeButton:(UIButton *)but
{
    HWLoginAndRegisterViewController *loginVC = [[HWLoginAndRegisterViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
- (void)shareButton:(UIButton *)but
{
    NSString *str = [NSString stringWithFormat:@"%@(来自@狮猿农奴畜)%@",_model.title,_model.url];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.cover_image_url]]];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"568be18b67e58e1b05000789" shareText:str shareImage:image shareToSnsNames:@[UMShareToSina,UMShareToRenren] delegate:self];
    
    
}
- (void)commentButton:(UIButton *)but
{
    HWCommentViewController *commentVC = [[HWCommentViewController alloc]init];
    commentVC.model = _model;
    [self.navigationController pushViewController:commentVC animated:YES];
}


- (void)addTableView
{
    _test = [[OTCover alloc]initWithTableViewWithHeaderImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.cover_image_url]]] withOTCoverHeight:200];
    _test.tableView.delegate = self;
    _test.tableView.dataSource = self;
    //test.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    //test.tableView.tableFooterView.backgroundColor = [UIColor cyanColor];
   
    _test.tableView.separatorStyle = UITableViewScrollPositionNone;
    [_test.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [self.view addSubview:_test];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = _model.title;
    tableView.rowHeight = _hegith;
    
    [cell addSubview:_webView];

    return cell;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    _hegith = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    _webView.frame = CGRectMake(0, 0, kScreenWidth, _hegith);
    
    
}
//添加手势是否隐藏bottomView
- (void)addTapgest
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
}
- (void)tap:(UITapGestureRecognizer *)tap
{
    
    
    if (!_isfirstTap) {
        _isfirstTap = YES;
        _footView.hidden = YES;
    }else{
        
        _footView.hidden = NO;
        _isfirstTap = NO;
    }

    
}
//滑动减速停止显示
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _footView.hidden = NO;
    
}
//开始拖拽影藏
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _footView.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
